package com.sniper.mybatis.typehandler;

import com.sniper.constants.Protocol;
import com.sniper.util.DateUtil;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;

/**
 * 时间转换
 *
 * @author suzhen
 * @create 2018/1/15
 */
public class LocalDateTimeHandler extends BaseTypeHandler<LocalDateTime> {
    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, LocalDateTime localDateTime, JdbcType jdbcType) throws SQLException {
        preparedStatement.setLong(i, DateUtil.getMills(localDateTime));
    }

    @Override
    public LocalDateTime getNullableResult(ResultSet resultSet, String s) throws SQLException {
        return DateUtil.getLocalDateTime(resultSet.getLong(s), Protocol.ZONE_ID);
    }

    @Override
    public LocalDateTime getNullableResult(ResultSet resultSet, int i) throws SQLException {
        return DateUtil.getLocalDateTime(resultSet.getLong(i), Protocol.ZONE_ID);
    }

    @Override
    public LocalDateTime getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        return DateUtil.getLocalDateTime(callableStatement.getLong(i), Protocol.ZONE_ID);
    }
}
